<template></template>
  
  <script setup>
import { ref, reactive, onMounted } from 'vue'
import { message } from 'ant-design-vue'
import { useRoute } from 'vue-router'
import { PrinterOutlined } from '@ant-design/icons-vue'
import { hiPrintPlugin, hiprint, defaultElementTypeProvider } from 'vue-plugin-hiprint-extend'
import panel from './design/panel'
import printData from './design/print-data'
import printFuns from './design/print-funs'
import Api from '@/api/request.js'
hiPrintPlugin.disAutoConnect() // 取消自动连接直接打印客户端
let hiprintTemplate
const route = useRoute()
const previewData = reactive({
  curPaper: {
    type: 'A4',
    width: 210,
    height: 296.6
  },
  waitShowPrinter: false
})
const initPrintTemplate = () => {
  // console.log(panel)
  // console.log(printData)
  // hiprintTemplate = new hiprint.PrintTemplate({
  //   template: panel
  // })
  // print(hiprintTemplate, printData)
  // return
  let FormKey = route.query.FormKey
  let OID = route.query.OID
  let PrintTemID = route.query.PrintTemID
  let CacheKey = route.query.CacheKey
  let url = 'WMS_PrintFunction'
  let params = JSON.stringify(['getPrintData', FormKey, OID, PrintTemID, CacheKey])
  Api.post(url, params)
    .then((res) => {
      console.log(res)
      if (res.error) {
        message.warning(res.error.error_info)
      } else {
        $('#hiprint-printTemplate').empty() // 先清空, 避免重复构建
        hiprintTemplate = new hiprint.PrintTemplate({
          template: res.data.Template
        })
        window.hiprint.hiprintExpData = res.data.hiprintExpData
        print(hiprintTemplate, res.data.printData)
      }
    })
    .catch((error) => {
      console.log(error)
      message.error(`请求异常`)
    })
}

const print = (hiprintTemplate, printData) => {
  previewData.waitShowPrinter = true
  hiprintTemplate.print(
    printData,
    {},
    {
      callback: () => {
        console.log('callback')
        previewData.waitShowPrinter = false
      }
    }
  )
}
const printTest = () => {
  var hiprintTemplate1 = new window.hiprint.PrintTemplate({
    template: panel
  })
  hiprintTemplate1.print(printData)
  console.log(hiprintTemplate.getHtml(printData))
  // window.document.body.innerHTML = `<div>12324</div>`
  //   window.print()
}
onMounted(() => {
  window.hiprintFuns = printFuns
  window.hiprint.query = route.query
  initPrintTemplate()
})
</script>
<style lang="scss" scoped>
</style>
<style>
.printview-container .hiprint-printPaper {
  border: 1px solid #000;
}
</style>